package org.opengauss.evaluate.utils;

import org.opengauss.evaluate.paeser.config.AssessmentInfoChecker;
import org.opengauss.evaluate.paeser.config.AssessmentInfoManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class ConnectionUtils {
    private static final Logger LOGGER = LoggerFactory.getLogger(ConnectionUtils.class);

    /**
     * Get jdbc connection.
     *
     * @param dbname : assessment database name.
     * @return Connection
     */
    public static Connection getConnection(String dbname) {
        String host = AssessmentInfoManager.getInstance().getProperty(AssessmentInfoChecker.OPENGAUSS,
                AssessmentInfoChecker.HOST);
        String port = AssessmentInfoManager.getInstance().getProperty(AssessmentInfoChecker.OPENGAUSS,
                AssessmentInfoChecker.PORT);
        String user = AssessmentInfoManager.getInstance().getProperty(AssessmentInfoChecker.OPENGAUSS,
                AssessmentInfoChecker.USER);
        String password = AssessmentInfoManager.getInstance().getProperty(AssessmentInfoChecker.OPENGAUSS,
                AssessmentInfoChecker.PASSWORD);
        String url = "jdbc:opengauss://" + host + ":" + port + "/" + dbname
                + "?useUnicode=false&characterEncoding=utf8&usessL=false";
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            LOGGER.warn("jdbc connect failed, maybe dolphin plugin does not create complete.");
        }
        return connection;
    }
}
